var costBalanceData = [];
var costBalanceBillList = [];
var BalanceBillNameList = [];
var flagDate = undefined;
var exportFlag = undefined;
var balanceBillName = undefined;
var showFlag = false;
var clickTime = 0;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	$("#export").attr('disabled', 'disabled');
	$ajaxList('searchBalanceInitSetting', getBalanceInitSetting);
    setTimeout(hideLoading, 500);
});
function getBalanceInitSetting(data){
	var balanceInitSettingData = data.balanceInitSettingList[0]
	$("#BalanceChargingPriceType").val(balanceInitSettingData.balanceChargingPriceType);
	var balanceStartMonth = balanceInitSettingData.balanceStartMonth
	changeYear(0, balanceStartMonth);
	showMonth(getSystemTime().getMonth() + 1);
}
function getBalanceBillName(data) {
	BalanceBillNameList = data || [];
	$("#BalanceBillID").empty();
	$("#BalanceBillID").append("<option value='' selected='selected'>请选择</option>");
	$("#ChargingPrice").text('');
	var length = data.length;
	if (length == 0) {
		$("#TotalTradeKWH").text(0);
		$("#TotalCost").text(0);
		$("#costBalanceBillThead").empty();
		$("#costBalanceBillTbody").empty();
		$("#ChargingPrice").text(messageContent["Balance.MonthBalanceBillNotExist"]);
		return;
	}
	for (var i = 0; i < length; i++) {
		$("#BalanceBillID").append("<option value='" + data[i].balanceBillID + "'>" + data[i].balanceBillName + "</option>");
	}
	search();
}
function getBalanceChargingPriceType(index) {
	$("#export").attr('disabled', 'disabled');
	if (index == 0) {
		$("#TotalTradeKWH").text(0);
		$("#TotalCost").text(0);
		$("#costBalanceBillThead").empty();
		$("#costBalanceBillTbody").empty();
		$("#ChargingPrice").text('');
	} else {
		var balanceChargingPriceType = BalanceBillNameList[index-1] && BalanceBillNameList[index-1].balanceChargingPriceType;
		exportFlag = balanceChargingPriceType;
		balanceBillName = BalanceBillNameList[index-1] && BalanceBillNameList[index-1].balanceBillName;
		if (balanceChargingPriceType == '1') {
			$("#BalanceChargingPriceType").val(balanceChargingPriceType);
			$("#ChargingPrice").text('固定电价：' + BalanceBillNameList[index-1].chargingFixedPrice + '元');
		} else if (balanceChargingPriceType == '2') {
			$("#BalanceChargingPriceType").val(balanceChargingPriceType);
			$("#ChargingPrice").text('平谷电价：' + '尖:' + BalanceBillNameList[index-1].chargingSharpPrice +'元， 峰:' + BalanceBillNameList[index-1].chargingPeakPrice + '元， 平:' + BalanceBillNameList[index-1].chargingFlatPrice + '元， 谷:' + BalanceBillNameList[index-1].chargingValleyPrice + '元');
		}
		search();
	}
}
function search(){
	$("#errorMessage").hide();
	$("#errorMessage").empty();
	if (!$("#BalanceBillID").val()) {
		$("#BalanceBillID").tips({
			side : 2,
			msg : "请选择电力结算单",
			bg : '#AE81FF',
			time : 1
		});
		$("#BalanceBillID").focus();
		return;
	}
	$ajax('searchCostBalanceBill', 'costBalanceBillSearch', searchCostBalanceBill);
}
function searchCostBalanceBill(data){
	$("#costBalanceBillThead").empty();
	var costBalanceBillTbody = $("#costBalanceBillTbody");
	costBalanceBillTbody.empty();
	$("#TotalTradeKWH").text(0);
	costBalanceBillList = data && data.costBalanceBillList || [];
	var length = costBalanceBillList.length;
	if (length == 0) {
		$("#ChargingPrice").text('');
		$ajaxList('balanceBillName', getBalanceBillName, {BalanceMonth: $("#balanceMonth").val()});
		$("#errorMessage").show();
		$("#errorMessage").html(messageContent["Balance.BalanceBillNotExist"]);
		return;
	}
	createTableHead("costBalanceBillThead", costBalanceBillTimeTable);
	
	var chargingStationID = undefined;
	var rowspanCount = undefined;
	var number = 0;
	$("#export").attr('disabled', false);
	for(var i=0; i<length + 1; i++){
		if (i == length || chargingStationID != costBalanceBillList[i].chargingStationID) {
			chargingStationID = costBalanceBillList[i] && costBalanceBillList[i].chargingStationID;
			if (rowspanCount) {
				for (var j=i - rowspanCount; j<i; j++) {
					$tr = $('<tr>').appendTo(costBalanceBillTbody);
					if (j==i - rowspanCount) {
						$('<td rowspan="'+ rowspanCount + '">').html(number).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[i-1].chargingStationName).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[i-1].checkMeterLastTime).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[i-1].checkMeterThisTime).appendTo($tr);
					}
					$('<td>').html(costBalanceBillList[j].checkMeterCode).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].checkMeterRate).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].totalMeterLastNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].sharpMeterLastNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].peakMeterLastNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].flatMeterLastNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].valleyMeterLastNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].idleTotalLastNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].totalMeterThisNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].sharpMeterThisNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].peakMeterThisNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].flatMeterThisNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].valleyMeterThisNo).appendTo($tr);
					$('<td>').html(costBalanceBillList[j].idleTotalThisNo).appendTo($tr);
					if (j==i - rowspanCount) {
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].totalActiveKWH).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].sharpActiveKWH).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].peakActiveKWH).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].flatActiveKWH).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].valleyActiveKWH).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].sharpProportion).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].peakProportion).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].flatProportion).appendTo($tr);
						$('<td rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].valleyProportion).appendTo($tr);
						
						$('<td class="hideTable" hidden="hidden" rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].payElectricCharge).appendTo($tr);
						$('<td class="hideTable" hidden="hidden" rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].totalChargingKWH).appendTo($tr);
						$('<td class="hideTable" hidden="hidden" rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].stationRatio).appendTo($tr);
						$('<td class="hideTable" hidden="hidden" rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].costSubtotal).appendTo($tr);
						$('<td class="hideTable" hidden="hidden" rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].difference).appendTo($tr);
						$('<td class="hideTable" hidden="hidden" rowspan="'+ rowspanCount + '">').html(costBalanceBillList[j].ratio).appendTo($tr);
					}
				}
			}
			rowspanCount = 1;
			number++;
		} else {
			rowspanCount++;
		}
	}
	setTimeout(hideLoading, 500);
}
function exportFile(){
	if (costBalanceBillList.length == 0){
		showMessge("没有数据可导出!")
	} else {
		showMessge("确定导出吗?", "model2");
	}
}
function showHideTable() {
	clickTime++;
	if(showFlag && clickTime == 5) {
		$(".hideTable").attr("hidden", false);
		showFlag = false;
		clickTime = 0;
	} else {
		$(".hideTable").attr("hidden", true);
		showFlag = true;
	}
}
function sure(){
	showMessge("导出中......");
	$ajax('exportCostBalanceBill?checkFlag=&', undefined, exportFileResult);
}
function exportFileResult(data){
	var result = data && data.result;
	if (result == "Success") {
		showLoading();
		downloadFile('exportCostBalanceBill?checkFlag=OK&');
		showMessge("导出成功，电力结算文件正在生成！");
		setTimeout(hideLoading, 2000);
	} else if (result == "JsonError") {
		showMessge("导出失败，导出的JSON模板错误 或 不存在！");
	} else if (result == "NotFound") {
		showMessge("导出失败，没有数据可导出！");
	} else {
		showMessge("导出失败，请重新导出 或 联系管理员！");
	}
}
function showMonth(index){
	$('.activeMonthLi').removeClass("activeMonthLi");
	$("#li"+ index).addClass("activeMonthLi");
	//执行查询时间起止方法
	var year = $("#showYear").html().replace(/[^0-9]/ig,"");
	index = index >= 10 ? index : "0" + index;
	$("#balanceMonth").val(year + index);
	$ajaxList('balanceBillName', getBalanceBillName, {BalanceMonth: $("#balanceMonth").val()});
	$("#export").attr('disabled', 'disabled');
}
function createNewBill(){
	$("#detailID", parent.document).val($("#balanceMonth").val());
	window.parent.skipSecondMenu('costManage/costBalanceBillCreate', 'costBalanceBill');
}